package main

import (
	"context"
	"fmt"
	"github.com/hashicorp/consul/api"
	order_pb "mxshop/api/order/v1"
	"mxshop/gmicro/registry/consul"
	rpc "mxshop/gmicro/server/rpcserver"
	"mxshop/gmicro/server/rpcserver/clientinterceptors"
	"mxshop/gmicro/server/rpcserver/selector"
	"mxshop/gmicro/server/rpcserver/selector/random"
	"mxshop/pkg/generateRandomString"
	"time"
)

func main() {
	//设置全局的负载均衡策略
	selector.SetGlobalSelector(random.NewBuilder())
	rpc.InitBuilder()

	conf := api.DefaultConfig()
	conf.Address = "192.168.16.192:8500"
	conf.Scheme = "http"
	cli, err := api.NewClient(conf)
	if err != nil {
		panic(err)
	}

	conn, err := rpc.DialInsecure(
		context.Background(),
		rpc.WithBanlancerName("selector"),
		rpc.WithDiscovery(consul.New(cli)),
		rpc.WithEndpoint("discovery:///shop-order-srv"),
		rpc.WithClientEnableTracing(false),
		rpc.WithClientUnaryInterceptor(clientinterceptors.UnaryTracingInterceptor),
		rpc.WithClientTimeout(time.Second*5000),
	)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	uc := order_pb.NewOrderClient(conn)

	_, err = uc.SubmitOrder(context.Background(), &order_pb.OrderRequest{
		UserId:  3,
		Address: "测试",
		Name:    "czc",
		Mobile:  "13067353692",
		Post:    "尽快发货",
		OrderSn: generateRandomString.GenerateSn(string(3)),
	})

	if err != nil {
		panic(err)
	}
	fmt.Println("订单创建成功!!")
}
